<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WebSocket</title>
</head>
<body>
  <b>
    WebSocket
  </b>
</body>
<script>
  // Create WebSocket connection.
  const socket = new WebSocket('wss://echo.websocket.org');

  // Connection opened
  socket.addEventListener('open', function (event) {
    console.log("建立连接成功");

    socket.send('Hello Server!');
  });

  // Listen for messages
  socket.addEventListener('message', function (event) {
    console.log('Message from server ', event.data);
  });

  socket.onopen = (evt)=>{
    console.log("建立连接成功");
  }
</script>
</html>